/* choices.c */

#include <string.h>

#include "choices.h"
#include "templates.h"
#include "bsound.h"

#include "kernel.h"

#include "DeskLib:Error.h"
#include "DeskLib:Event.h"
#include "DeskLib:File.h"
#include "DeskLib:Handler.h"
#include "DeskLib:Icon.h"
#include "DeskLib:Screen.h"
#include "DeskLib:WimpSWIs.h"
#include "DeskLib:Window.h"


#define SWI_Cache_Control 0x280

BOOL choices_nicekeys;
scrn_type choices_display;

static window_handle choices_window;

static char choices_filename[256];

/* Icon numbers */
typedef enum {
  choices_UPDATE,
  choices_SAVE,
  choices_CANCEL,
  choices_BKEYS = 5,
  choices_CKEYS,
  choices_STANDARD = 9,
  choices_ECOVGA,
  choices_DLVGA,
  choices_1CHAN = 14,
  choices_2CHAN,
  choices_4CHAN,
  choices_8CHAN,
  choices_NOSOUND
} choices_icons;

/* Initialise choices; if file not found use defaults */
window_handle choices_init(char *filename)
{
  file_handle fp;

  strcpy(choices_filename,filename);
  fp = File_Open(filename,file_READ);
  if (fp)
  {
    choices_nicekeys = File_Read32(fp);
    choices_display = File_Read32(fp);
    SoundChannels = File_Read32(fp);
    File_Close(fp);
  }
  else
  {
    choices_nicekeys = TRUE;
    /* 2 OS units per vert. pixel in VGA, 4 in Standard */
    if (screen_mode != 22 && screen_eig.y < 2)
    {
      /* Find out if processor is cached (fast enough for de-luxe VGA) */
      _kernel_swi_regs r;

      r.r[0] = 0;
      r.r[1] = 0xffffffff;
      if ((BOOL) _kernel_swi(SWI_Cache_Control,&r,&r))
        choices_display = scrn_single_vga;
      else
        choices_display = scrn_double_vga;
    }
    else
      choices_display = scrn_standard;
  }

  /* Create window from template */
  choices_window = templates_create("Choices");

  /* Register handlers */
  Event_Claim(event_CLICK,choices_window,choices_UPDATE,choices_update,0);
  Event_Claim(event_CLICK,choices_window,choices_SAVE,choices_save,0);
  Event_Claim(event_CLICK,choices_window,choices_CANCEL,choices_cancel,0);

  choices_writeicons(choices_nicekeys,choices_display);

  return choices_window;
}

void choices_writeicons(BOOL keys,scrn_type display)
{
  Icon_SetSelect(choices_window,choices_BKEYS,keys);
  Icon_SetSelect(choices_window,choices_CKEYS,!keys);
  switch (display)
  {
    case scrn_standard:
      Icon_SetRadios(choices_window,choices_STANDARD,choices_DLVGA,
      		     choices_STANDARD);
      break;
    case scrn_single_vga:
      Icon_SetRadios(choices_window,choices_STANDARD,choices_DLVGA,
      		     choices_ECOVGA);
      break;
    case scrn_double_vga:
      Icon_SetRadios(choices_window,choices_STANDARD,choices_DLVGA,
      		     choices_DLVGA);
      break;
  }
  switch (SoundChannels)
  {
    case 0:
      Icon_SetRadios(choices_window, choices_1CHAN, choices_NOSOUND,
      	choices_NOSOUND);
      break;
    case 1:
      Icon_SetRadios(choices_window, choices_1CHAN, choices_NOSOUND,
      	choices_1CHAN);
      break;
    case 2:
      Icon_SetRadios(choices_window, choices_1CHAN, choices_NOSOUND,
      	choices_2CHAN);
      break;
    case 4:
      Icon_SetRadios(choices_window, choices_1CHAN, choices_NOSOUND,
      	choices_4CHAN);
      break;
    case 8:
      Icon_SetRadios(choices_window, choices_1CHAN, choices_NOSOUND,
      	choices_8CHAN);
      break;
  }
}

void choices_readoptions(BOOL *keys,scrn_type *display)
{
  *keys = Icon_WhichRadioInEsg(choices_window,1);
  switch (*keys)
  {
    case -1:
    case choices_BKEYS:
      *keys = TRUE;
      break;
    case choices_CKEYS:
      *keys = FALSE;
      break;
  }
  *display = Icon_WhichRadioInEsg(choices_window,2);
  if (*display == -1)
    *display = scrn_standard;
  else
    *display -= choices_STANDARD;
  switch (Icon_WhichRadioInEsg(choices_window, 3))
  {
    case choices_NOSOUND:
      SoundChannels = 0;
      break;
    case choices_2CHAN:
      SoundChannels = 2;
      break;
    case choices_4CHAN:
      SoundChannels = 4;
      break;
    case choices_8CHAN:
      SoundChannels = 8;
      break;
    default:
      SoundChannels = 1;
      break;
  }
}

BOOL choices_update(event_pollblock *event,void *ref)
{
  if (event->data.mouse.button.data.menu)
    return FALSE;
  choices_readoptions(&choices_nicekeys,&choices_display);
  if (event->data.mouse.button.data.select)
    Wimp_CreateMenu((menu_ptr) -1,0,0);
  else
    choices_writeicons(choices_nicekeys,choices_display);
  return TRUE;
}

BOOL choices_save(event_pollblock *event,void *ref)
{
  file_handle fp;

  if (event->data.mouse.button.data.menu)
    return FALSE;
  choices_update(event,ref);
  fp = File_Open(choices_filename,file_WRITE);
  if (fp)
  {
    File_Write32(fp,choices_nicekeys);
    File_Write32(fp,choices_display);
    File_Write32(fp,SoundChannels);
    File_Close(fp);
  }
  return TRUE;
}

BOOL choices_cancel(event_pollblock *event,void *ref)
{
  Wimp_CreateMenu((menu_ptr) -1,0,0);
  return TRUE;
}
